package com.block.product.service.union;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.service.LogFactory;
import com.block.common.util.RSAUtil;
import com.block.common.vo.ResponseVo;
import com.block.db.common.vo.BaseUserInfoVo;
import com.block.db.entity.ProductInfo;
import com.block.product.service.IUnionProductService;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 今日必下 联登  业务
 */
@Service
public class BXTodayProductServiceImpl implements IUnionProductService {

    private static Logger log = LogFactory.getCommLog();

    private static final String MATCH_USER_URL = "https://weeb.lcwlkd.top/api/v1/union/unionCheckUser";
//    private static final String MATCH_USER_URL = "https://ddb-test.gujishuke.com/ddb/external/check/user";

    private static final String REGISTER_USER_URL = "https://weeb.lcwlkd.top/api/v1/union/unionRegister";
//    private static final String REGISTER_USER_URL = "https://ddb-test.gujishuke.com/ddb/external/union/register";

    @Override
    public ResponseVo matchUserInfo(ProductInfo productInfo, BaseUserInfoVo userInfoVo) {

        try {

            HttpRequest httpRequest = HttpUtil.createPost(MATCH_USER_URL);
            httpRequest.header("Content-Type", "application/json");

            JSONObject bizParamObject = new JSONObject();
            bizParamObject.put("phone", userInfoVo.getMobile());

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("appId", productInfo.getChannelKey());
            paramMap.put("data", RSAUtil.rsaBASE64Encode(JSON.toJSONString(bizParamObject), productInfo.getChannelSecret()));

            httpRequest.body(JSON.toJSONString(paramMap));
            String resultStr = httpRequest.execute().body();
            log.info("【产品联登】【今日必下】【撞库】撞库完成【resultStr】{}【MATCH_USER_URL】{}【paramMap】{}", resultStr, MATCH_USER_URL, JSON.toJSONString(paramMap));

            JSONObject jsonObject = JSON.parseObject(resultStr);
            Integer code = jsonObject.getInteger("code");
            String msg = jsonObject.getString("msg");
            boolean data = jsonObject.getBoolean("data");
            if (ObjectUtil.equals(Integer.valueOf("200"), code) && ObjectUtil.equal(true, data)){
                return new ResponseVo(AppRspCodeConstant.SUCCESS);
            }
            return new ResponseVo(AppRspCodeConstant.THRID_FAILED);
        }catch (Exception exception){
            log.error("【产品联登】【今日必下】【撞库】撞库异常【userInfoVo】" + JSON.toJSONString(userInfoVo), exception);
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION);
        }
    }

    @Override
    public ResponseVo registerUserInfo(ProductInfo productInfo, BaseUserInfoVo userInfoVo) {

        try {

            HttpRequest httpRequest = HttpUtil.createPost(REGISTER_USER_URL);
            httpRequest.header("Content-Type", "application/json");

            JSONObject bizParamObject = new JSONObject();
            bizParamObject.put("phone", userInfoVo.getMobile());

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("appId", productInfo.getChannelKey());
            paramMap.put("data", RSAUtil.rsaBASE64Encode(JSON.toJSONString(bizParamObject), productInfo.getChannelSecret()));

            httpRequest.body(JSON.toJSONString(paramMap));
            String resultStr = httpRequest.execute().body();
            log.info("【产品联登】【今日必下】【注册】注册完成【result】{}【MATCH_USER_URL】{}【paramMap】{}", resultStr, REGISTER_USER_URL, JSON.toJSONString(paramMap));

            JSONObject jsonObject = JSON.parseObject(resultStr);
            Integer code = jsonObject.getInteger("code");
            String msg = jsonObject.getString("msg");
            if (!ObjectUtil.equals(Integer.valueOf("200"), code)){
                return new ResponseVo(AppRspCodeConstant.THRID_FAILED, msg, "");
            }

            String data = jsonObject.getString("data");
            JSONObject resultData=  new JSONObject();
            resultData.put("productUrl", data);

            return new ResponseVo(AppRspCodeConstant.SUCCESS, resultData);
        }catch (Exception exception){
            log.error("【产品联登】【今日必下】【注册】注册异常【userInfoVo】" + JSON.toJSONString(userInfoVo), exception);
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION);
        }
    }

    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
    }

}
